package com.sugon.modules.tools.service.impl;

import cn.hutool.core.date.DateUtil;
import com.sugon.cache.J2CacheUtils;
import com.sugon.dao.ToolsListDao;
import com.sugon.entity.*;
import com.sugon.modules.tools.service.IToolsStorePosManager;
import com.sugon.service.ToolsListService;
import com.sugon.service.ToolsPartOutService;
import com.sugon.service.ToolsStorePosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ： YuXD
 * @description： 备件库存业务Manager实现类
 * @date ： 2020-09-01 下午 4:14
 * @version: 1.0
 */
@Service
public class ToolsStorePosManagerImpl implements IToolsStorePosManager {

    @Autowired
    private ToolsListService toolsListService;
    @Autowired
    private ToolsStorePosService toolsStorePosService;
    @Autowired
    private ToolsPartOutService toolsPartOutService;
    @Autowired
    private ToolsListDao toolsListDao;

    /**
     * stockOut
     *
     * @param materialBase
     * @param materialItems 备件清单列表
     * @return
     */
    @Override
    public boolean stockOut(MaterialBaseEntity materialBase, List<MaterialRecordEntity> materialItems) {
        // 备件领料清单出库
        for (MaterialRecordEntity materialItem : materialItems) {
            int num = materialItem.getNum();
            String toolsCode = materialItem.getSpareCode();
            String posId = materialItem.getPostionId();
            //获取仓库位置库存信息
            ToolsStorePosEntity pos = toolsStorePosService.queryObject(posId);
            // 获取备件信息
            ToolsListEntity toolsList = toolsListService.queryByCode(toolsCode);
            //增加出库记录
            //增加出库记录
            ToolsPartOutEntity tpo = new ToolsPartOutEntity();
            tpo.setSystemCode(toolsList.getErpCode());
            tpo.setOutCode(J2CacheUtils.getSequence(4, J2CacheUtils.TOOLS_OUT));
            tpo.setOutDate(DateUtil.date());
            tpo.setUseUnit(materialBase.getDeptId());
            tpo.setPartCode(toolsList.getToolsCode());
            tpo.setName(toolsList.getName());
            tpo.setSpec(toolsList.getType());
            tpo.setOutNum(num);
            tpo.setRelNum(num);
            tpo.setCheckStatus("1");
            tpo.setApplyTime(materialBase.getPickDate());
            tpo.setApplyUser(materialBase.getPicker());
            tpo.setPartId(materialBase.getId());
            tpo.setPostionId(posId);
            tpo.setIsDel(1);
            int rows = toolsPartOutService.save(tpo);
            if (rows > 0) {
                /*
//                1、生成备件报废记录
                1、更新下工装库位的锁定数和最新库存数
                2、更新下工装的库存数
                 */
//                // 1、生成报废记录
//                scarpListService.doScrap(out);
                // 2、实时更新备件库位的库存和锁定库存数
                toolsStorePosService.realtimeUpdateStorePosStock(posId);
                // 3、实时更新备件库存数
                toolsListDao.realtimeUpdateStock(toolsCode);
            }
        }
        return true;
    }
}
